﻿using System;
using System.Collections;
using System.Collections.Generic;
using offline2048;
using UnityEditor;
using UnityEngine;

namespace GTA
{
    public class GameDataMgr : MonoSingleton<GameDataMgr>
    {
        public long lastSaveDataTime = 0;
        public bool IsDebugDevice = false;
        public long firstInstallTime = 0;
        public string firstInstallVersion = "";
        public string gameDataVersion = "1.0.0";
        public string currentGameVersion = "1.0.0";
        public int userGroupId = 0;
        // public int launchTimes = 0;
        public int iapSuccessTimes = 0;
        // public int todayLaunchTimes = 0;
        public long lastLaunchTime = 0;
        public long lastCloseTime = 0;
        // public bool isNoAds = false;
        public bool isNotifcation = false;
        public int installTimes = 1;
        public bool closeAppByAd { get; private set; } = false;
        public long weekFirstPlayTime = 0;
        public int weekFirstStartLvId = 0;
        public bool unlockShowPiggyBank = true;
        bool isInstall = false;
        public int todayEnterGameTimes = 0;
        public long lastEnterGameTime = 0;
        int curEnterGameIADTimes = 0;
        public bool isShowIOSATT = false;

        private Coroutine _coroutine;
        public void SetCloseAppByAd(bool isclose) 
        {
            closeAppByAd = isclose;
            if (_coroutine != null) 
            {
                StopCoroutine(_coroutine);
                _coroutine = null;
            }
            if (isclose)
            {
                _coroutine = StartCoroutine(SetCloseState(1, () =>
                {
                    closeAppByAd = false;
                }));
            }
        }

        private IEnumerator SetCloseState(float delay, Action action)
        {
            yield return new WaitForSeconds(delay);
            if (action != null) 
            {
                action();
            }
        }

        /// <summary>
        /// 刷新每周首次起始关卡id
        /// </summary>
        /// <param name="levelId"></param>
        public void RefreshWeekFirstData(int levelId)
        {
            long nowTime = GTA.Utils.GetTimeStamp();
            if (weekFirstPlayTime == 0 || !GTA.Utils.IsInSameWeek(weekFirstPlayTime, nowTime))
            {
                weekFirstPlayTime = GTA.Utils.GetTimeStamp();
                weekFirstStartLvId = levelId;
            }
        }

        /// <summary>
        /// 检测唤醒次数
        /// </summary>
        public void CheckLaunchCnt()
        {

        }

        public void SetGroupId(int groupId) {
            userGroupId = groupId;
            SaveData();
        }

        public void SetTestDevice() {
            IsDebugDevice = true;
            SaveData();
        }

        public void AddIapTimes() {
            iapSuccessTimes ++;
            SaveData();
        }

        public bool IsInstall() {
            return isInstall;
        }
     
        public void SaveData()
        {
            lastSaveDataTime = Utils.GetTimeStamp();
            DataMgr.Instance.SaveData();
        }

        public void AddEnterGameTimes(int cnt)
        {
            CheckUpdateDayData();
            todayEnterGameTimes += cnt;
            if (todayEnterGameTimes < 0)
            {
                todayEnterGameTimes = 0;
            }
            lastEnterGameTime = Utils.GetTimeStamp();
            SaveData();
        }

        public int GetCurEnterIADTimes()
        {
            return curEnterGameIADTimes;
        }

        public void AddCurEnterIADTimes()
        {
            curEnterGameIADTimes++;
        }

        public void ParseData(string data)
        {


        }

        public string ParseToString() 
        {
            return JsonUtility.ToJson(this);
        }

        /// <summary>
		/// 目标版本小于等于初始安装版本
		/// </summary>
		/// <param name="version"></param>
		/// <returns></returns>
        public bool IsLessOrEqualFirstInstallVersion(string version)
        {
            int[] targetVersionNumber = Array.ConvertAll<string, int>(version.Split('.'), value => Convert.ToInt32(value));
            int[] installVersionNumber = Array.ConvertAll<string, int>(firstInstallVersion.Split('.'), value => Convert.ToInt32(value));
            if (targetVersionNumber.Length == installVersionNumber.Length)
            {
                if (targetVersionNumber[0] < installVersionNumber[0])
                {
                    return true;
                }
                else if (targetVersionNumber[0] == installVersionNumber[0]
                    && targetVersionNumber[1] < installVersionNumber[1])
                {
                    return true;
                }
                else if (targetVersionNumber[0] == installVersionNumber[0]
                    && targetVersionNumber[1] == installVersionNumber[1]
                    && targetVersionNumber[2] <= installVersionNumber[2])
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 检测是否跨天，更新数据
        /// </summary>
        public void CheckUpdateDayData()
        {
            if (!Utils.IsSameDay(lastEnterGameTime))
            {
                todayEnterGameTimes = 0;
            }
        }
    }
}